package com.gensdai.leliang.utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.storage.StorageManager;
import android.text.TextUtils;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.gensdai.leliang.Encrypt.DesEncrypt;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.Locale;


/**
 * 一些获取系统信息的工具类
 *
 * @author Administrator
 */
public class BaseUtils {

    /**
     * 获取系统语言
     * 语言 中文为zh，英文为en，日文为ko
     *
     * @return
     */
    public static String getDefaultLanguage() {
        String language = null;

        language = Locale.getDefault().getLanguage();

        return language;
    }

    /**
     * 格式化TextView的显示格式，识别手机号
     *
     * @param textView
     * @param source
     * @return
     */
//    private SpannableStringBuilder formatPhoneNumber(TextView textView, String source) {
//        // 若要部分 SpannableString 可点击，需要如下设置
//        textView.setMovementMethod(LinkMovementMethod.getInstance());
//        // 将要格式化的 String 构建成一个 SpannableStringBuilder
//        SpannableStringBuilder value = new SpannableStringBuilder(source);
//
//        // 使用正则表达式匹配电话
//        Linkify.addLinks(value, Pattern.compile(PATTERN_PHONE), SCHEME_TEL);
//
//        // 获取上面到所有 addLinks 后的匹配部分(这里一个匹配项被封装成了一个 URLSpan 对象)
//        URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class);
//        for (final URLSpan urlSpan : urlSpans) {
//            if (urlSpan.getURL().startsWith(SCHEME_TEL)) {
//                int start = value.getSpanStart(urlSpan);
//                int end = value.getSpanEnd(urlSpan);
//                value.removeSpan(urlSpan);
//                value.setSpan(new ClickableSpan() {
//                    @Override
//                    public void onClick(View widget) {
//                        String phone = urlSpan.getURL().replace(SCHEME_TEL, "");
//                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//                        builder.setMessage("是否拨打电话：" + phone);
//                        builder.setNegativeButton("取消", null);
//                        builder.setPositiveButton("确定", null);
//                        builder.create().show();
//                    }
//
//                    @Override
//                    public void updateDrawState(TextPaint ds) {
//                        super.updateDrawState(ds);
//                        ds.setColor(Color.parseColor("#3f8de2"));
//                        ds.setUnderlineText(true);
//                    }
//                }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            }
//        }
//        return value;
//    }


    //des加密 加密格式就代码
    public static String EncryptedPassword(String pass) {
        String encryptPass = DesEncrypt.encrypt(pass, DesEncrypt.DES_PRIVATE_ENCRYPT_KEY);
        String resultPass = null;
        if (!TextUtils.isEmpty(encryptPass)) {
            String replaceChar = encryptPass.replace("+","[j]");  //替换所有+号为[j]
//            if (replaceCharIndex > 0) {
//                String resultOne = encryptPass.substring(0, replaceCharIndex);
//                String resultTwo = encryptPass.substring(replaceCharIndex + 1, encryptPass.length());
//                resultPass = new StringBuffer();
//                resultPass.append(resultOne);
//                resultPass.append("[j]");
//                resultPass.append(resultTwo);
//            } else {
                resultPass = replaceChar;
//            }
        }

        return resultPass.toString();
    }


    //格式化浮点
    public static String formatFloat(float flo) {
        String count = new DecimalFormat("###,###,###.##").format(flo);
        return count;
    }
    public static String formatDouble(double flo) {
        String count = new DecimalFormat("###,###,###.##").format(flo);
        return count;
    }


    public static boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return false;
        } else {
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {

                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }



    public static PipelineDraweeController resetPic(Uri uri,DraweeController controll){
        int width = 200, height = 200;
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setOldController(controll)
                .setImageRequest(request)
                .build();

        return controller;
    }


    /*获取外部SD卡路径 扩展内存就是我们插入的外置SD卡*/
    public static String getExtendedMemoryPath(Context mContext) {
        StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        Class<?> storageVolumeClazz = null;
        try {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
            Method getPath = storageVolumeClazz.getMethod("getPath");
            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
            Object result = getVolumeList.invoke(mStorageManager);
            final int length = Array.getLength(result);
            for (int i = 0; i < length; i++) {
                Object storageVolumeElement = Array.get(result, i);
                String path = (String) getPath.invoke(storageVolumeElement);
                boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
                if (removable) {
                    return path;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
